sap.ui.define([
    "sap/ui/core/mvc/Controller",
    "sap/ui/model/json/JSONModel",
    "c090/model/Formatter"
],
    /**
     * @param {typeof sap.ui.core.mvc.Controller} Controller
     */
    function (Controller, JSONModel, Formatter) {
        "use strict";


        return Controller.extend("c090.controller.Main", {
            formatter: Formatter,
            onInit: function () {
                var oModel = new JSONModel(sap.ui.require.toUrl("c090/model/Product.json"));
                var oModel2 = new JSONModel(sap.ui.require.toUrl("c090/model/Employees.json"));
                this.getView().setModel(oModel);
                this.getView().setModel(oModel2, "emp");
                // 创建包含数量的新 JSON 对象
                var countResults = {
                    "PriceLessThan20": 0,
                    "Price20To40": 0,
                    "PriceGreaterThan40": 0
                };

                oModel.attachRequestCompleted(function () {
                    var oData = oModel.getData();

                    // 计算数量
                    oData.Products.forEach(product => {
                        if (product.Price < 20) {
                            countResults.PriceLessThan20++;
                        } else if (product.Price >= 20 && product.Price < 40) {
                            countResults.Price20To40++;
                        } else {
                            countResults.PriceGreaterThan40++;
                        }
                    });

                    this.getView().setModel(new JSONModel(countResults), "counts");
                }.bind(this));
            },
            onSelectFilter: function (oEvent) {
                var sKey = oEvent.getParameter("key")
                new sap.m.MessageToast.show("Selected filter: " + sKey);
            },
            addNewButtonPressHandler: function () {

                // 假设要添加的新数据对象
                var newEmployee = {
                    name: "New",
                    empFirstName: "New",
                    empLastName: "Employee",
                    position: "New Position",
                    icon: "sap-icon://accessibility",
                    iconTooltip: "new",
                    salary: 1500.00
                };

                var oModel2 = this.getView().getModel("emp");
                // 获取当前模型的数据
                var currentData = oModel2.getData();

                // 添加新数据对象到数组中
                currentData.employees.push(newEmployee);

                // 设置更新后的数据回到模型中
                oModel2.setData(currentData);


                // var newEmployee = new sap.m.TabContainerItem({
                //     name: "New",
                //     additionalText: "Developer",
                //     icon: "sap-icon://group",
                //     iconTooltip: "group",
                //     modified: false
                // });

                var tabContainer = this.byId("myTabContainer");
                tabContainer.getItems().some(function (item) {
                    if (item.getName() === newEmployee.name) {
                        tabContainer.setSelectedItem(item);
                        return true; // 停止遍历
                    }
                });

            },
            itemCloseHandler: function (oEvent) {
                // prevent the tab being closed by default
                oEvent.preventDefault();

                var oTabContainer = this.byId("myTabContainer");
                var oItemToClose = oEvent.getParameter('item');

                sap.m.MessageBox.confirm("Do you want to close the tab '" + oItemToClose.getName() + "'?", {
                    onClose: function (oAction) {
                        if (oAction === sap.m.MessageBox.Action.OK) {
                            oTabContainer.removeItem(oItemToClose);
                            sap.m.MessageToast.show("Item closed: " + oItemToClose.getName(), { duration: 500 });
                        } else {
                            sap.m.MessageToast.show("Item close canceled: " + oItemToClose.getName(), { duration: 500 });
                        }
                    }
                });
            }
        });
    });
